package com.example.c94s3plyspringboot.web;

import com.example.c94s3plyspringboot.po.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

/**
 * 参数相关注解
 *
 * @RequestParam 请求参数映射
 *      1. 设置参数名映射关系
 *      2. 设置参数是否必选
 *      3. 设置默认值
 * @RequestHeader 头域参数映射
 * @CookieValue Cookie参数映射
 * @PathVariable 地址参数
 * Servlet对象注入
 *      HttpServletRequest
 *      HttpServletResponse
 *      HttpSession
 *      InputStream 请求对象的输入流
 *      OutputStream 相应体中的输出流
 *      .... 请求参考官方
 */
@RestController
public class 参数映射 {

    // http://127.0.0.1/param1?name=100
    @RequestMapping("param1")
    public String param(@RequestParam("name") int a) {
        return "" + a;
    }

    @RequestMapping("param2")
    public String param2(@RequestParam(required = false) Integer a) {
        return "" + a;
    }

    @RequestMapping("param3")
    public String param3(@RequestParam(defaultValue = "100") Integer a) {
        return "" + a;
    }

    @RequestMapping("param4")
    public String param4(@RequestHeader("Host") String host) {
        return host;
    }

    @RequestMapping("param5")
    public String param5(@CookieValue String name, @CookieValue(name="age") String nainling
            , @CookieValue(required = false, defaultValue = "女") String gender) {
        return name + "  " + nainling + "  " + gender;
    }

    /**
     * 1. 这是Spring的接口注入方式
     * 2. SpringMVC 要求尽量避免注入 Servlet 对象
     * @param request
     * @param response
     * @param session
     * @param in
     * @param out
     * @return
     */
    @RequestMapping("param6")
    public String param6(
            HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session,
            InputStream in,
            OutputStream out
    ){
        return request.toString() + "<br>" +
                response.toString() + "<br>" +
                session.toString() + "<br>" +
                in.toString() + "<br>" +
                out.toString() + "<br>";
    }

    /**
     * RESTful请求方式 : http : method get , post, delete, put, option
     *   传统方式   /login?name=zhangsan&pwd=123
     *   地址参数   /zhangsan/123/login
     */
    @GetMapping("login/{name}/{password}")
    public User login(@PathVariable String name, @PathVariable("password") String pwd,
                      HttpSession session){
        User user = new User();
        user.setName(name);
        user.setPwd(pwd);
        // 将用户对象存入会话  AJAX请求,写会话属性, 必须通过会话对象
        session.setAttribute("loginedUser", user);
        session.setAttribute("loginedTime", new Date());
        return user;
    }

    @GetMapping("getLoginedUser")
    public String getLoginedUser(
            @SessionAttribute User loginedUser,
            @SessionAttribute("loginedTime") Date date,
            @SessionAttribute(name = "loginedType", required = false) Integer type){
        return loginedUser + "<br>" +
                date + "<br>" +
                type + "<br>";
    }


}
